#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeacherCourseProvider : CY.GFive.Core.Providers.DALProvider.ITeacherCourseProvider
	{
		#region Sql
		private static readonly string SqlSelectTeacherCourse = "SELECT [Id], [TeacherCode],[CourseCode] FROM [TeacherCourse] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeacherCourse = "INSERT INTO [TeacherCourse]([TeacherCode],[CourseCode]) values(@TeacherCode,@CourseCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeacherCourse = "UPDATE [TeacherCourse] SET [TeacherCode] = @TeacherCode,[CourseCode] = @CourseCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeacherCourse = "DELETE [TeacherCourse] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeacherCourse = "SELECT [Id], [TeacherCode],[CourseCode] FROM [TeacherCourse]";
		#endregion

		#region ITeacherCourseProvider Members
		public Core.Business.TeacherCourse Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeacherCourse);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TeacherCourse teacherCourse = new Core.Business.TeacherCourse();

				if (!reader.IsDBNull(0)) teacherCourse.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) teacherCourse.TeacherCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) teacherCourse.CourseCode = reader.GetString(2);

				return teacherCourse;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TeacherCourse teacherCourse)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, teacherCourse.TeacherCode);
			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, teacherCourse.CourseCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeacherCourse);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teacherCourse.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TeacherCourse teacherCourse)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, teacherCourse.TeacherCode);
			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, teacherCourse.CourseCode);
			sql.AddParameter("@Id", SqlDbType.Int, teacherCourse.Id);
			sql.ExecuteSql(SqlUpdateTeacherCourse);
		}

		public void Delete(Core.Business.TeacherCourse teacherCourse)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teacherCourse.Id);
			sql.ExecuteSql(SqlDeleteTeacherCourse);
		}

		public IList<Core.Business.TeacherCourse> GetAllTeacherCourse()
		{
			IList<Core.Business.TeacherCourse> teacherCourselist = new List<Core.Business.TeacherCourse>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeacherCourse);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TeacherCourse teacherCourse = new Core.Business.TeacherCourse();

					if (!reader.IsDBNull(0)) teacherCourse.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) teacherCourse.TeacherCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) teacherCourse.CourseCode = reader.GetString(2);

					teacherCourse.MarkOld();
					teacherCourselist.Add(teacherCourse);
				}
				reader.Close();
			}
			return teacherCourselist;
		}
		#endregion
	}
}
